﻿namespace com.stifel.BranchMgmtDao
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using com.stifel.Extensions.SharePointExtensions;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Linq;

    using Context = BranchMgmtDataContext;

    using CustomObject = StifelTelephoneSystem;

    public class StifelTelephoneSystemDao : StifelBaseDao
    {
        #region Fields

        public const string ListName = "stifeltelephonesystem";

        #endregion Fields

        #region Constructors

        public StifelTelephoneSystemDao( )
            : base(ListName)
        {
            CurrentWebUrl = ConfiguredWebUrl;
        }

        public StifelTelephoneSystemDao( string currentWebUrl )
            : base(ListName)
        {
            this.CurrentWebUrl = currentWebUrl;
        }

        #endregion Constructors

        #region Methods

        public CustomObject Add( CustomObject value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                string title = value.Title;
                List<SPFile> attachments = value.Attachments;
                Guid g = Guid.NewGuid( );
                value.Title = g.ToString( );
                l.InsertOnSubmit( clean( value ) );
                c.SubmitChanges( );
                CustomObject v = ( from o in l
                                   where o.Title == g.ToString( )
                                   select o ).SingleOrDefault( );
                v.Title = title;
                c.SubmitChanges( );

                if ( attachments != null && attachments.Count > 0 )
                    v.ListItem.AddAttachmentNow( attachments );

                return v;
            }
        }

        public void Clear( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                var v = ( from o in l
                          where o.Id > 0
                          select o );
                if ( v.Count( ) > 0 )
                {
                    l.DeleteAllOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public void Delete( List<Guid> values )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                foreach ( Guid g in values )
                {
                    CustomObject v = ( from o in l
                                       where o.UniqueId == g
                                       select o ).SingleOrDefault( );
                    if ( v != null )
                        l.DeleteOnSubmit( v );
                }
                c.SubmitChanges( );
            }
        }

        public CustomObject Get( int id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.Id.HasValue && o.Id.Value == id
                                   select o ).SingleOrDefault( );
                return v;
            }
        }

        public List<CustomObject> GetAll( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                List<CustomObject> l = c.GetList<CustomObject>( ListTitle ).ToList( );
                return l;
            }
        }

        public List<CustomObject> GetByBranchLocation( int id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                List<CustomObject> v = ( from o in l
                                         where o.BranchLookupId.HasValue && o.BranchLookupId == id
                                         select o ).ToList( );
                return v;
            }
        }

        public List<CustomObject> GetExpiring( DateTime target )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                List<CustomObject> v = new List<CustomObject>( );
                //TODO:
                // List<CustomObject> v = ( from o in l where o.Expires.HasValue && o.Expires.Value.CompareTo( target ) <= 0 select o ).ToList( );
                return v;
            }
        }

        public CustomObject Update( CustomObject value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.UniqueId == value.UniqueId
                                   select o ).SingleOrDefault( );
                if ( v != null )
                {
                    v.BranchLookupId = value.BranchLookupId;
                    v.ContractExpiration = value.ContractExpiration;
                    v.TelephoneCircuitIds = value.TelephoneCircuitIds;
                    v.TelephoneEquipment = value.TelephoneEquipment;
                    v.TelephoneEquipmentNotification = value.TelephoneEquipmentNotification;
                    v.TelephoneMaintenanceContract = value.TelephoneMaintenanceContract;
                    v.Title = value.Title;
                    // commit changes
                    c.SubmitChanges( );
                    return v;
                }
                else
                    return null;
            }
        }

        CustomObject clean( CustomObject value )
        {
            return new CustomObject
            {
                BranchLookupId = value.BranchLookupId,
                ContractExpiration = value.ContractExpiration,
                TelephoneCircuitIds = value.TelephoneCircuitIds,
                TelephoneEquipment = value.TelephoneEquipment,
                TelephoneEquipmentNotification = value.TelephoneEquipmentNotification,
                TelephoneMaintenanceContract = value.TelephoneMaintenanceContract,
                Title = value.Title
            };
        }

        #endregion Methods
    }
}